package com.css.dzj.zfjg.modules.creditassess.controller;

import com.alibaba.fastjson.JSON;
import com.css.dzj.zfjg.common.audit.service.SysAuditService;
import com.css.dzj.zfjg.common.util.BeanUtil;
import com.css.dzj.zfjg.common.util.Constant;
import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.modules.creditassess.entity.JgLhjgCreditAssessEntity;
import com.css.dzj.zfjg.modules.creditassess.service.CreditassessService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 企业信用评估信息controller
 */
@RestController
@RequestMapping("/creditassess")
public class JgLhjgCreditAssessController {

    @Autowired
    private CreditassessService creditassessService;

    // base日志接口
    @Autowired
    SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;

    /**
     * 添加企业信用评估信息详情
     *
     * @param jgLhjgCreditAssessEntity
     * @return
     */
    @PostMapping("/add")
    public R addJgLhjgCreditAssessEntity(@RequestBody JgLhjgCreditAssessEntity jgLhjgCreditAssessEntity, HttpServletRequest request) {
        String s = "";
        try {
            if (StringUtils.isEmpty(jgLhjgCreditAssessEntity)) {
                return R.error("企业信用评估信息不能为空");
            }
            if (StringUtils.isEmpty(jgLhjgCreditAssessEntity.getEnterpriseIdType()) || StringUtils.isEmpty(jgLhjgCreditAssessEntity.getEnterpriseCreditCode()) || StringUtils.isEmpty(jgLhjgCreditAssessEntity.getEnterpriseMark()) ||
                    StringUtils.isEmpty(jgLhjgCreditAssessEntity.getEnterpriseType())) {
                return R.error("必填字段不能为空");
            }
            creditassessService.add(jgLhjgCreditAssessEntity, request);
            s = JSON.toJSONString(jgLhjgCreditAssessEntity);
            sysAuditService.saveSuccessAudit("企业信用评估信息添加成功", Constant.ZFJG_CREDITASSESS, Constant.PT, Constant.ZFJG_ADD, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("企业信用评估信息添加失败", Constant.ZFJG_CREDITASSESS, Constant.YZ, Constant.ZFJG_ADD, s, e, request);
            return R.error(e.getMessage());
        }

    }

    /**
     * 更新企业信用评估信息详情
     *
     * @param jgLhjgCreditAssessEntity
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody JgLhjgCreditAssessEntity jgLhjgCreditAssessEntity) {
        String s = "";
        try {
            if (StringUtils.isEmpty(jgLhjgCreditAssessEntity)) {
                return R.error("企业信用评估信息不能为空");
            }
            JgLhjgCreditAssessEntity old = creditassessService.findById(jgLhjgCreditAssessEntity.getCreditAssessId());
            BeanUtil.copyBeanNotEmptyToBean(jgLhjgCreditAssessEntity, old);
            /*jgLhjgCreditAssessEntity.setColumnTime(new Date());*/
            creditassessService.update(jgLhjgCreditAssessEntity);
            s = JSON.toJSONString(jgLhjgCreditAssessEntity);
            sysAuditService.saveSuccessAudit("企业信用评估信息修改成功", Constant.ZFJG_CREDITASSESS, Constant.PT, Constant.ZFJG_UPDATE, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(jgLhjgCreditAssessEntity);
            sysAuditService.saveFailAudit("企业信用评估信息修改失败", Constant.ZFJG_CREDITASSESS, Constant.YZ, Constant.ZFJG_UPDATE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 通过enterpriseCreditCode查询企业信息评估信息详情
     * @param enterpriseCreditCode
     * @return
     */
    @GetMapping("/queryById")
    public R queryById(@RequestParam String enterpriseCreditCode) {
        String s = "";
        try {
            JgLhjgCreditAssessEntity jgLhjgCreditAssessEntity = creditassessService.findById(enterpriseCreditCode);
            s = JSON.toJSONString(enterpriseCreditCode);
            sysAuditService.saveSuccessAudit("通过id获取企业信用评估信息成功", Constant.ZFJG_CREDITASSESS, Constant.PT, Constant.ZFJG_SELECT, s, request);
            return R.ok().put("data", jgLhjgCreditAssessEntity);
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(enterpriseCreditCode);
            sysAuditService.saveFailAudit("通过id获取企业信用评估信息失败", Constant.ZFJG_CREDITASSESS, Constant.YZ, Constant.ZFJG_SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 查询所有企业信用评估信息详情
     *
     * @param pageSize
     * @param pageNo
     * @return
     */
    @GetMapping("/queryAll")
    public R queryAll(@RequestParam Integer pageSize, @RequestParam Integer pageNo) {
        String s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<JgLhjgCreditAssessEntity> jgLhjgCreditAssessEntityQueryResults = creditassessService.queryAll(pageRequest);
            s = JSON.toJSONString(jgLhjgCreditAssessEntityQueryResults);
            sysAuditService.saveSuccessAudit("分页查询所有企业信用评估信息成功", Constant.ZFJG_CREDITASSESS, Constant.PT, Constant.ZFJG_PAGE, s, request);
            return R.ok().put("data", jgLhjgCreditAssessEntityQueryResults);
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("分页查询所有企业信用评估信息失败", Constant.ZFJG_CREDITASSESS, Constant.YZ, Constant.ZFJG_PAGE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 多条件分页查询企业信用评估信息详情
     *
     * @param pageSize
     * @param pageNo
     * @param enterpriseName
     * @param enterpriseIdType
     * @param enterpriseType
     * @return
     */
    @GetMapping("listJgLhjgCreditAssessEntity")
    public R listJgLhjgCreditAssessEntity(@RequestParam Integer pageSize, @RequestParam Integer pageNo,
                                          @RequestParam String enterpriseName, @RequestParam String enterpriseIdType,
                                          @RequestParam String enterpriseType) {
        String s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<JgLhjgCreditAssessEntity> jgLhjgCreditAssessEntityQueryResults = creditassessService.listJgLhjgCreditAssessEntity(pageRequest, enterpriseName, enterpriseIdType, enterpriseType);
            s = JSON.toJSONString(jgLhjgCreditAssessEntityQueryResults);
            sysAuditService.saveSuccessAudit("多条件分页查询企业信用评估信息成功", Constant.ZFJG_CREDITASSESS, Constant.PT, Constant.ZFJG_PAGE, s, request);
            return R.ok().put("data", jgLhjgCreditAssessEntityQueryResults);
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("多条件分页查询企业信用评估信息失败", Constant.ZFJG_CREDITASSESS, Constant.YZ, Constant.ZFJG_PAGE, s, e, request);
            return R.error(e.getMessage());
        }
    }


    @GetMapping("/delete")
    public R delete(@RequestParam String id) {
        String s = "";
        try {
            if (StringUtils.isEmpty(id)) {
                return R.error("信用评估信息id不能为空");
            }
            JgLhjgCreditAssessEntity jgLhjgCreditAssessEntity = creditassessService.findById(id);
            if (StringUtils.isEmpty(jgLhjgCreditAssessEntity)) {
                return R.error("企业信用评估信息不能为空");
            }
            creditassessService.delete(jgLhjgCreditAssessEntity);
            s = JSON.toJSONString(jgLhjgCreditAssessEntity);
            sysAuditService.saveSuccessAudit("企业信用评估信息删除成功", Constant.ZFJG_CREDITASSESS, Constant.PT, Constant.ZFJG_DELETE, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(id);
            sysAuditService.saveFailAudit("企业信用评估信息删除失败", Constant.ZFJG_CREDITASSESS, Constant.YZ, Constant.ZFJG_PAGE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 信用信息库对外接口查询企业信用评估信息列表
     *
     * @param enterpriseName
     * @param enterpriseMark1
     * @param enterpriseMark2
     * @param enterpriseType
     */
    @GetMapping("/creditinfoList")
    public List creditinfoList(@RequestParam String enterpriseName, @RequestParam String enterpriseMark1, @RequestParam String enterpriseMark2, @RequestParam String enterpriseType) {
        String s = "";
        try {
            List<JgLhjgCreditAssessEntity> list = creditassessService.creditInfo(enterpriseName, enterpriseMark1, enterpriseMark1, enterpriseType);
            s = JSON.toJSONString(list);
            sysAuditService.saveSuccessAudit("查询成功", Constant.ZFJG_CREDITASSESS, Constant.PT, Constant.ZFJG_SELECT, s, request);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("查询失败", Constant.ZFJG_CREDITASSESS, Constant.YZ, Constant.ZFJG_SELECT, s, e, request);
            return null;
        }
    }

    /**
     * 企业评估信息总数
     */
    @GetMapping("/gettotalnum")
    public String totalnum() {
        Integer resulte = creditassessService.gettotalnum();
        String result = resulte.toString();
        return result;
    }

    /**
     * 导出
     * 导出选中行数据，待定
     *
     * @param response
     * @param ids
     * @return
     */
    @GetMapping("/exportByIds")
    public List exportCreditAssessToExcel(HttpServletResponse response, @RequestParam String ids) {
        List<JgLhjgCreditAssessEntity> creditAssessEntities = creditassessService.queryByIds(ids);
        return creditAssessEntities;
    }

    /**
     * 根据参数导出数据到Excel
     *
     * @param enterpriseName
     * @param enterpriseMark
     * @param enterpriseType
     * @return
     */
    @GetMapping("/export")
    public List export(@RequestParam(required = false) String enterpriseName,
                    @RequestParam(required = false) String enterpriseMark,
                    @RequestParam(required = false) String enterpriseType) {
        List<JgLhjgCreditAssessEntity> jgLhjgCreditAssessEntities = creditassessService.queryByParams(enterpriseName, enterpriseMark,
                enterpriseType);
        return jgLhjgCreditAssessEntities;
    }


}
